package com.juntuo.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.juntuo.Enums.Constants;
import com.juntuo.entity.Company;
import com.juntuo.entity.User;
import com.juntuo.service.CompanyService;
import com.juntuo.shiro.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 对片区 街道 泊位等进行管理
 * @author chen xj
 * @create 2017/03/03 11:04
 */
@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @RequestMapping("/index")
    public String toIndex(){
        return "/company/index";
    }


    @RequestMapping("/list")
    @ResponseBody
    public List<Company> getPage(){
        User user = ShiroUtil.currentUser();
        if(user.getCompanyId()!=null){
            return null;
        }else{
            return companyService.selectAll();
        }

    }

    @RequestMapping("/page")
    @ResponseBody
    public PageInfo<Company> getPage(int pageNumber, int pageSize, String name){
        User user = ShiroUtil.currentUser();
        if(user.getCompanyId()!=null){
            return null;
        }else{
            return companyService.findPagedCompany(pageNumber,pageSize,name);
        }

    }

    @RequestMapping("/save")
    @ResponseBody
    public boolean saveOne(Company company){
        if(company.getId()==null || company.getId()==0){
            company.setCreateTime(new Date());
            company.setStatus(Constants.COMPANY_ACTIVE);
            companyService.insert(company);
        }else{
            company.setUpdateTime(new Date());
            companyService.updateByPrimaryKeySelective(company);
        }

        return true;
    }


    @RequestMapping("/delete/{id}")
    @ResponseBody
    public int deleteOne(@PathVariable int id){
        Company company = new Company();
        company.setId(id);
        company.setStatus(Constants.COMPANY_DELETED);
        companyService.updateByPrimaryKeySelective(company);
        return 1;
    }





}
